﻿using System;
using System.Collections.Generic;
using ThirdPartyLib.DotLiquidTemplatingDemo.Models;
using ThirdPartyLib.DotLiquidTemplatingDemo.Services;

namespace ThirdPartyLib.DotLiquidTemplatingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            /******************************************************************
            **
            **  DotLiquid is another great templating language and would be my
            **  my preference for projects in .NET 3.5 as Razor is not avail-
            **  able. The only problem I have with DotLiquid is that it requires
            **  a base class on all the objects being passed into it. I feel
            **  that this is a stupid requirement. I wrote a method that will 
            **  convert an object into a compatible hash dictionary. This lets
            **  me pass any POCO object in as a model. 
            **  
            ******************************************************************/

            var model = new Order
            {
                ReferenceNumber = 12345,
                CustomerEmail = "steve.jobs@apple.com",
                ShippingAddress = new Address
                {
                    Number = 1234,
                    Street = "Applesauce Lane",
                    City = "Silicon Valley",
                    State = State.CA
                },
                Products = new List<Product>
                {
                    new Product {Id = 1, Name = "Google Stock", Price = 250.00M},
                    new Product {Id = 1, Name = "Microsoft Stock", Price = 150.00M},
                }
            };

            var template = @"
                <html>
                <body>
                    <div>Reference Id: {{ReferenceNumber}}</div>
                    <p>Email: {{CustomerEmail}}</p>
                    <p>Address:<br />
                        {{ShippingAddress.Number}} {{ShippingAddress.Street}}<br />
                        {{ShippingAddress.City}}, {{ShippingAddress.State}}
                    </p>
                    <table>
                        <tr><td>Id</td><td>Product Name</td><td>Price</td>
                        {% for p in Products -%}
                        <tr><td>{{p.Id}}</td><td>{{p.Name}}</td><td>${{p.Price | format_as_money}}</td>
                        {% endfor -%}
                    </table>
                </body>
                </html>
            ";

            //Normally I would bring this in via dependency injection
            ITemplatingService templatingService = new TemplatingService();
            var result = templatingService.Render(template, model);

            //Breakpoint here and check out the result in the HTML Visualizer
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}
